package com.gitee.sidihuo.utils.util;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class TimeFormatter {

    public static String PATTERN_STANDARD = "yyyyMMddHHmmss";

    /**
     * 20240121140242
     * 14位 yyyyMMddHHmmss
     */
    public static DateTimeFormatter FORMATTER_STANDARD = DateTimeFormatter.ofPattern(PATTERN_STANDARD);

    //=========================
    //----------------------------------------------------------------------------------------------------------
    //=========================


    public static String PATTERN_SHORT = "yyMMddHHmmssSSS";
    /**
     * 240121140242329
     * 15位 yyMMddHHmmssSSS
     */
    public static DateTimeFormatter FORMATTER_SHORT = DateTimeFormatter.ofPattern(PATTERN_SHORT);

    //=========================
    //----------------------------------------------------------------------------------------------------------
    //=========================

    public static String PATTERN_VIEW_MILLS = "yyyy-MM-dd HH:mm:ss.SSS";
    /**
     * 2024-01-21 14:09:37.598
     * yyyy-MM-dd HH:mm:ss.SSS
     */
    public static DateTimeFormatter FORMATTER_VIEW_MILLS = DateTimeFormatter.ofPattern(PATTERN_VIEW_MILLS);

    //=========================
    //----------------------------------------------------------------------------------------------------------
    //=========================


    public static String PATTERN_VIEW = "yyyy-MM-dd HH:mm:ss";
    /**
     * 2024-01-21 14:09:37
     * yyyy-MM-dd HH:mm:ss
     */
    public static DateTimeFormatter FORMATTER_VIEW = DateTimeFormatter.ofPattern(PATTERN_VIEW);

    //=========================
    //----------------------------------------------------------------------------------------------------------
    //=========================


    /**
     * 20240121140242
     * 14位 yyyyMMddHHmmss
     */
    public static String formatCurrent() {
        return formatCurrent(FORMATTER_STANDARD);
    }

    /**
     * 20240121
     * 8位 yyyyMMdd
     */
    public static String formatCurrentDate() {
        return formatCurrent(DateTimeFormatter.BASIC_ISO_DATE);
    }

    /**
     * 2024-01-21
     * yyyy-MM-dd
     */
    public static String formatCurrentDateView() {
        return formatCurrent(DateTimeFormatter.ISO_LOCAL_DATE);
    }

    /**
     * 14:26:12.482
     * HH:mm:ss.SSS
     */
    public static String formatCurrentOnlyTimeMillsView() {
        return formatCurrent(DateTimeFormatter.ISO_LOCAL_TIME);
    }

    public static String formatCurrent(DateTimeFormatter dateTimeFormatter) {
        LocalDateTime now = LocalDateTime.now();
        if (dateTimeFormatter == null) {
            dateTimeFormatter = FORMATTER_STANDARD;
        }
        String format = now.format(dateTimeFormatter);
        return format;
    }

    public static LocalDateTime parseLocalDateTime(String formatString, DateTimeFormatter dateTimeFormatter) {
        LocalDateTime localDateTime = LocalDateTime.parse(formatString, dateTimeFormatter);
        return localDateTime;
    }

    public static void main(String[] args) {
        System.out.println(formatCurrent());
        System.out.println(formatCurrent(FORMATTER_SHORT));
        System.out.println(formatCurrent(FORMATTER_VIEW_MILLS));
        System.out.println(formatCurrent(FORMATTER_VIEW));

        System.out.println("=========");

        System.out.println(formatCurrentDate());
        System.out.println(formatCurrentDateView());
        System.out.println(formatCurrentOnlyTimeMillsView());

        System.out.println("--------");
        System.out.println(parseLocalDateTime(formatCurrent(), FORMATTER_STANDARD));
        System.out.println(parseLocalDateTime(formatCurrent(FORMATTER_SHORT), FORMATTER_SHORT));

    }
}